//
//  EarningsDetailsTableViewCell.swift
//  LeTu
//
//  Created by g1 on 2023/12/30.
//

import UIKit

class EarningsDetailsTableViewCell: BaseTableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            _ = self.lineView.layer.sublayers?.map { $0.removeFromSuperlayer() }
            self.lineView.addDashedLine(color: .app_lineColor, dashedLineWidth: 6, lineHeight: CGFloat.app_lineHieght)
        }
    }
    static func getCellHeight() -> CGFloat {
        return 70
    }
    func setRecordModel(_ recordModel: EarningsRecordModel) {
        titleLabel.text = Utils.hideMiddleDigitsInPhoneNumber(recordModel.subLevelUserMobile) + (LanguageManager.shared.currentLanguage == .zh ? recordModel.description:recordModel.englishDescription)
        timeLabel.text = recordModel.createTime.components(separatedBy: " ").first ?? recordModel.createTime
        priceLabel.text = (recordModel.incomeType == 0 ? "+":"-") + "\(recordModel.incomeMoney.formattedString())"
        priceLabel.textColor = recordModel.incomeType == 0 ? .app_priceTextColor:.app_black
        
        /// 0:未转账 1:已转账 2:已拒绝
        statusLabel.isHidden = recordModel.incomeLevel != 2
        statusLabel.text = recordModel.withdrawStatusText
        statusLabel.textColor = recordModel.withdrawStatus == 0 ? UIColor.hex(hexString: "#00FF7F"):UIColor.hex(hexString: "#FF6347")
        statusLabel.snp.updateConstraints { make in
            make.height.equalTo(recordModel.withdrawStatusText.count > 0 ? 18 : 0)
        }
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(backView)
        backView.addSubview(titleLabel)
        backView.addSubview(timeLabel)
        backView.addSubview(rightContentView)
        rightContentView.addSubview(priceLabel)
        rightContentView.addSubview(statusLabel)
        contentView.addSubview(lineView)
        
        backView.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.centerY.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.height.equalTo(24)
        }
        timeLabel.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.height.equalTo(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
        }
        rightContentView.snp.makeConstraints { make in
            make.centerY.right.equalToSuperview()
            make.left.equalTo(timeLabel.snp.right).offset(12)
        }
        priceLabel.snp.makeConstraints { make in
            make.right.left.top.equalToSuperview()
            make.height.equalTo(22)
        }
        statusLabel.snp.makeConstraints { make in
            make.top.equalTo(priceLabel.snp.bottom)
            make.right.left.bottom.equalToSuperview()
            make.height.equalTo(0)
        }
        lineView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.right.equalTo(backView)
            make.height.equalTo(CGFloat.app_lineHieght)
        }
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        return backView
    }()
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangMedium(14)
        return titleLabel
    }()
    private lazy var timeLabel: UILabel = {
        let timeLabel = UILabel()
        timeLabel.textColor = .app_detaisltextColor
        timeLabel.font = kSetPingFangRegular(12)
        return timeLabel
    }()
    private lazy var rightContentView = UIView()
    private lazy var priceLabel: UILabel = {
        let priceLabel = UILabel()
        priceLabel.font = kSetPingFangMedium(15)
        priceLabel.textAlignment = .right
        return priceLabel
    }()
    private lazy var statusLabel: UILabel = {
        let priceLabel = UILabel()
        priceLabel.font = kSetPingFangMedium(12)
        priceLabel.textAlignment = .right
        return priceLabel
    }()
    private lazy var lineView = UIView()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
